//
//  ZXXVisitorView.swift
//  Swift_Weibo
//
//  Created by 朱骁雄 on 16/4/9.
//  Copyright © 2016年 朱骁雄. All rights reserved.
//

import UIKit

//定义代理协议
protocol ZXXVisitorViewDelegate : NSObjectProtocol{
    
    //定义代理方法
    func visitorViewDidClickRegisterButton()
    
    func visitorViewDidClickLoginButton()
    
}





class ZXXVisitorView: UIView {
    
    //定义代理属性
    weak var delegate: ZXXVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景颜色
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startRotation(){
        //使用核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.duration = 10
        
        //动画完成不移除动画
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
        
        
    }
    
    
    
    func setupVisitorViewInfo(imageName: String, message:String){
        
        iconView.image = UIImage(named: imageName)
        
        textLabel.text = message
        
        houseView.hidden = true
        
        coverView.hidden = true
    }
    
    // 更新UI
    private func setupUI (){
        
        //添加控件
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(textLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //禁用Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        
        
        //添加约束
        //        iconView.frame = CGRect(x: self.frame.size.width*0.5, y: self.frame.size.height*0.5, width: 300, height: 300)
        
        // 约束都往父控件添加
        /// item: 要约束的view
        /// attribute: 要约束的view的什么属性
        /// relatedBy: 参照关系
        /// toItem: 要参照的view
        /// attribute: 参照的view的什么属性
        
        //        转轮
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //        遮盖
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Bottom, relatedBy: .Equal, toItem: textLabel, attribute: .Bottom, multiplier: 1, constant: 0))
        
        //房子
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //文本
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1, constant: 16))
        //当view为nil时
        addConstraint(NSLayoutConstraint(item: textLabel, attribute:.Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 250))
        
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Leading, relatedBy: .Equal, toItem: textLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        /// 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
    }
    
    //MARK: - 自定义代理方法
    
    //定义代理方法
   @objc func didClickRegisterButton(){
        
         delegate?.visitorViewDidClickRegisterButton()

    }
    
    
   @objc func didClickLoginButton(){
         delegate?.visitorViewDidClickLoginButton()

    }
    
    
    
    //MARK: - 懒加载
    
    //转盘
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮盖
    private lazy var coverView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //房子
    private lazy var houseView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字
    
    private lazy var textLabel : UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人!看看有神马惊喜!"
        
        label.font = UIFont.systemFontOfSize(18)
        
        label.numberOfLines = 1
        
        label.textAlignment = NSTextAlignment.Center
        
        return label
    }()
    
    //登陆按钮
    private lazy var  loginButton : UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登陆", forState: .Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.blackColor(), forState: .Normal)
        
        button.addTarget(self, action: Selector("didClickLoginButton"), forControlEvents: .TouchUpInside)
        
        return button
        
    }()
    
    
    
    //注册按钮
    private lazy var registerButton : UIButton = {
        
        let button = UIButton()
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        button.addTarget(self, action: Selector("didClickRegisterButton"), forControlEvents: .TouchUpInside)
        return button
    }()
}
